MIN_INT_ABS = '2147483648'; #abs
MAX_INT_ABS = '2147483647'; #abs
MIN_INTEGER = -2147483648; #abs
MAX_INTEGER = 2147483647; #abs

def integer(s):
    """

    parse str into integer
    Cases: 1. overflow 2. invalid chars
    :type num: str
    :rtype: int
    """

    started = False;
    digits = [];
    number = 0;
    minus = False;
    for letter in s:
        if letter.isspace() and not started:
            continue;
        elif (letter == '+' or letter == '-') and not started:
            started = True;
            if letter == '-':
                minus = True;
        elif letter.isnumeric():
            if not started:
                started = True;
            digits.append(letter);
            overflow, limit = check(digits, minus);
            if not overflow:
                number = number * 10 + int(letter);
            elif limit != 0:
                return limit;
            else:
                break;
        else:
            break;
    if started and len(digits) > 0:
        if minus:
            number = -number;
        print("Valid", number);
    else:
        print('No valid input');
    return number;

def check(digits, minus):
    length = len(digits);
    maxLen = len(MIN_INT_ABS);
    if length < maxLen:
        return False, 0;
    elif length > maxLen:
        if minus:
            return True, MIN_INTEGER;
        else:
            return True, MAX_INTEGER;
    else:
        if minus:
            if ''.join(digits) >= MIN_INT_ABS:
                return True, MIN_INTEGER;
            else:
                return False, 0;
        else:
            if ''.join(digits) >= MAX_INT_ABS:
                return True, MAX_INTEGER;
            else:
                return False, 0;
